﻿using System.Collections.Generic;
using System.Text;

namespace Close.io.API
{
	public abstract class QueryBuilderBase
	{
		 

		protected Dictionary<string, string> parameters;

		protected QueryBuilderBase(string leadId)
		{
			 

			parameters = new Dictionary<string, string>();

			if (!string.IsNullOrWhiteSpace(leadId))
				parameters.Add("lead_id", leadId);
		}

		public string LeadId { get; set; }

		internal void Add(string key, string value)
		{
			Remove(key);
			parameters.Add(key, value);
		}

		internal void Remove(string key)
		{
			if (parameters.ContainsKey(key))
				parameters.Remove(key);

		}

		public Dictionary<string, string> ToParameterDictionary()
		{
			return parameters;
		}

		public override string ToString()
		{
			var buffer = new StringBuilder();

			foreach (var queryParameter in parameters)
			{
				if (buffer.Length == 0)
					buffer.AppendFormat("{0}={1}", queryParameter.Key, queryParameter.Value);
				else
				{
					buffer.AppendFormat("&{0}={1}", queryParameter.Key, queryParameter.Value);
				}
			}
			buffer.Insert(0, "?");
			return buffer.ToString();
		}
	}
}